﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Model.Data;
using Core.Model.Data.Generics;
using Exceptions;

namespace Core.Model
{
    public class Colour
    {

        //The Red, Green and Blue code values in the range 0 to 255. The Transparency byte codes
        //values in the range 0 to 255 representing transparency. 0 is opaque.
        public const int MAX_VALUE = 255;
        public const int MIN_VALUE = 0;

        private int red = 0;
    
        public int Red
        {
            get
            {
                return this.red;
            }
            set
            {
                if (value >= MIN_VALUE && value <= MAX_VALUE )
                {
                    this.red = value;   
                } 
                else
                    throw new OpenMhegException("Invalid value for colour sub element Red:"+value+" Should be between "+MIN_VALUE+" and "+MAX_VALUE+".");
            }
        }

        private int green = 0;

        public int Green
        {
            get
            {
                return this.green;
            }
            set
            {
                if (value >= MIN_VALUE && value <= MAX_VALUE)
                {
                    this.green = value;
                }
                else
                    throw new OpenMhegException("Invalid value for colour sub element Green:" + value + " Should be between " + MIN_VALUE + " and " + MAX_VALUE + ".");

            }
        }
        
        private int blue = 0;

        public int Blue
        {
            get
            {
                return this.blue;
            }
            set
            {
                if (value >= MIN_VALUE && value <= MAX_VALUE)
                {
                    this.blue = value;
                }
                else
                    throw new OpenMhegException("Invalid value for colour sub element Blue:" + value + " Should be between " + MIN_VALUE + " and " + MAX_VALUE + ".");

            }
        }        
        
        private int alpha = 255;

        public int Alpha
        {
            get
            {
                return this.alpha;
            }
            set
            {
                if (value >= MIN_VALUE && value <= MAX_VALUE)
                {
                    this.alpha = value;
                }
                else
                    throw new OpenMhegException("Invalid value for colour sub element Alpha:" + value + " Should be between " + MIN_VALUE + " and " + MAX_VALUE + ".");

            }
        }

        public Colour()
        {

        }

        public Colour(int red, int green, int blue, int alpha)
        {
            setComponents(red,green,blue,alpha);
        }

        public Colour(int index)
        {
            throw new OpenMhegException("No CLUT defined in 1.06");
        }

        public Colour(string colour)
        {
            LookupColour((new ASCIIEncoding()).GetBytes(colour));
        }

        public Colour(byte[] components)
        {
            LookupColour(components);
        }

        public Colour(OctetString colour  )
        {
            LookupColour(colour.Value);
        }


        private void setComponents(int red, int green, int blue, int alpha)
        {
            this.Red = red;
            this.Green = green;
            this.Blue = blue;
            this.Alpha = alpha;
        }


        private void LookupColour(byte[] components)
        {

            //if (components.Length < 1 || components.Length > 4)
            if (components.Length != 4)
                throw new OpenMhegException("Wrong number of components when building Colour:" + components.Length + ". Should be 4");
            
                this.Red = components[0]; 

                if (components.Length > 1)
                {
                    this.Green = components[1];
                }

                if (components.Length > 2)
                {
                    this.Blue = components[2];
                }

                if (components.Length > 3)
                {
                    this.Alpha = components[3];
                    
                }

            
        }

        private void LookUpColour(string components)
        {
            //if ( name.Equals( "white", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.White;
            //else if ( name.Equals( "gray", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Gray;
            //else if ( name.Equals( "darkgray", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkGray;
            //else if ( name.Equals( "lightgray", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.LightGray;
            //else if ( name.Equals( "black", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Black;
            //else if ( name.Equals( "red", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Red;
            //else if ( name.Equals( "darkred", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkRed;
            //else if ( name.Equals( "blue", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Blue;
            //else if ( name.Equals( "darkblue", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkBlue;
            //else if ( name.Equals( "green", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Green;
            //else if ( name.Equals( "darkgreen", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkGreen;
            //else if ( name.Equals( "yellow", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Yellow;
            //else if ( name.Equals( "darkyellow", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Yellow;
            //else if ( name.Equals( "cyan", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Cyan;
            //else if ( name.Equals( "darkcyan", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkCyan;
            //else if ( name.Equals( "magenta", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Magenta;
            //else if ( name.Equals( "darkmagenta", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkMagenta;
            //else if ( name.Equals( "orange", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Orange;
            //else if ( name.Equals( "pink", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Pink;
            //else if ( name.Equals( "transparent", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.FromArgb( 0, 0, 0, 0 );
            //else if ( name.StartsWith( "#" ) && name.Length == 7 )
            //{
            //    throw new Exception( "Code me" );
            //    //r = DecodeUtils.ParseHex( name.substring( 1, 3 ), 16 );
            //    //g = DecodeUtils.ParseHex( name.substring( 3, 5 ), 16 );
            //    //b = DecodeUtils.ParseHex( name.substring( 5, 7 ), 16 );
            //    //colour = new Color( r, g, b );
            //}
            //else if ( name.StartsWith( "=" ) && name.Length == 10 )
            //{
            //    throw new Exception( "Code me" );
            //    //r = DecodeUtils.ParseHex( name.Substring( 2, 4 ));
            //    //g = DecodeUtils.ParseHex( name.substring( 5, 7 ));
            //    //b = DecodeUtils.ParseHex( name.substring( 8, 10 ));
            //    //a = 255 - Builder.parseInt( name.substring( 11, 13 ), 16 );
            //    //colour = new Color( r, g, b, a );
            //}
            //else if ( name.Length == 3 )
            //{
            //    r = ( int ) name [0];
            //    g = ( int ) name [1];
            //    b = ( int ) name [2];
            //    colour = Color.FromArgb( r, g, b );
            //}
            //else if ( name.Length == 4 )
            //{
            //    r = ( int ) name [0];
            //    g = ( int ) name [1];
            //    b = ( int ) name [2];
            //    a = 255 - ( int ) name [3];
            //    //a = ( int ) name [3];
            //    colour = Color.FromArgb( a, r, g, b );
            //}
            //  else
            //  {
        }

    }
}
